/*
 * contact for contact/index
 * */

jQuery(document).ready(function() {		
	
	/* Contact Form */
	if(jQuery('#contactform').length != 0){
		addForm('#contactform');
	}
	
	/* Google Maps */
	loadGoogleMaps();
});

// Gooogle Map
function initGoogleMaps() {
	/* Google Maps Init */
	var myLatlng = new google.maps.LatLng(16.069321,108.217499);
	var myOptions = {
		zoom: 14,
		center: myLatlng,
		popup: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("googlemap"), myOptions);
	
	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map,
		title:"CLB Thiện Nguyện Vô Ưu"
	});
	google.maps.event.addListener(marker, 'click', function() {
		map.setZoom(17);
	});
}
  
function loadGoogleMaps() {
	/* Google Maps Load */
	if(jQuery('#googlemap').length != 0){
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "/js/sala/private/googlemap.js";//http://maps.google.com/maps/api/js?sensor=false&callback=initGoogleMaps
		document.body.appendChild(script);
	}
}


/* Contact Form */
function addForm(formtype) {

	var formid = jQuery(formtype);
	var contactsend = false;
	
	formid.find("input[name=SEND]").click(sendcontact);
	
	function validator() {
		
		var emailcheck =/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var othercheck = /.{4}/;
		var noerror = true;
		
		formid.find(".requiredfield").each(function () {
													 
			var fieldname = jQuery(this).attr('name');
			var value = jQuery(this).val();
			
			
			if(fieldname == "EMAIL"){
				if (!emailcheck.test(value)) {
					jQuery(this).addClass("formerror");
					noerror = false;
				}else{
					jQuery(this).removeClass("formerror");
				}	
			}
			
			if(fieldname == "PHONE"){
				if(!isNaN(value) && othercheck.test(value)){
					jQuery(this).removeClass("formerror");
				}else{
					jQuery(this).addClass("formerror");	
					noerror = false;
				}
			}
			if(fieldname == "NAME"){
				if (!othercheck.test(value)) {
					jQuery(this).addClass("formerror");
					noerror = false;
				} else {
					jQuery(this).removeClass("formerror");
				}	
			}
			if(fieldname == "CONTENT"){
				if (!othercheck.test(value)) {
					jQuery(this).addClass("formerror");
					noerror = false;
				} else {
					jQuery(this).removeClass("formerror");
				}	
			}
			/*else{
				if (!othercheck.test(value)) {
					jQuery(this).addClass("formerror");
					noerror = false;
				} else {
					jQuery(this).removeClass("formerror");
				}	
			}*/
		})
		
		if(!noerror){
			formid.find(".errormessage").fadeIn();
		}
		
		return noerror;
	}
	
	function resetform() {
		formid.find("input[type=text]").each(function () {
			jQuery(this).val("");	
		})
		formid.find("textarea").val("");
		contactsend = false;
	}
	
	function sendcontact() {
		formid.find(".successmessage").hide();
		var phpfile = "";
		
		if(formtype=="#contactform"){
			phpfile = "/contact/index/save";
		}else{
			phpfile = "";
		}
		
		if (validator()) {
			if(!contactsend){
				contactsend = true;
				jQuery.post(phpfile, formid.serialize(), function() {
					formid.find(".sendingmessage").hide();
					formid.find(".successmessage").fadeIn();
					resetform();
				});
			}
		} 
		return false
	}
	
	/*function sendemail() {
	formid.find(".successmessage").hide();
	var phpfile = "";
	if(formtype=="#contactform"){
		phpfile = "/contact/index/save";
	}
	else if(formtype=="#newsletterform"){
		phpfile = "php/signup.php";
	}else{
		phpfile = "";
	}
	if (validator()) {
		if(!emailsend){
			emailsend = true;
			formid.find(".errormessage").hide();
			formid.find(".sendingmessage").show();
			jQuery.post(phpfile, formid.serialize(), function() {
				formid.find(".sendingmessage").hide();
				formid.find(".successmessage").fadeIn();
				resetform();
			});
		}
	} 
	return false
}*/
}